using System.Text;
using Newtonsoft.Json;

namespace Mafool.Core;

public static partial class Extends
{
    /// <summary>
    /// 类型转换 -> Int
    /// </summary>
    public static int ToInt(this string s)
    {
        if (string.IsNullOrEmpty(s)) return 0;
        _ = int.TryParse(s, out int ret);
        return ret;
    }
    
    /// <summary>
    /// 类型转换 -> Long
    /// </summary>
    public static long ToLong(this string s)
    {
        if (string.IsNullOrEmpty(s)) return 0;
        _ = long.TryParse(s, out long ret);
        return ret;
    }

    /// <summary>
    /// 类型转换 -> Double
    /// </summary>
    public static double ToDouble(this string s)
    {
        if (string.IsNullOrEmpty(s)) return 0;
        _ = double.TryParse(s, out double ret);
        return ret;
    }

    /// <summary>
    /// 类型转换 -> Decimal
    /// </summary>
    public static decimal ToDecimal(this string s)
    {
        if (string.IsNullOrEmpty(s)) return 0;
        _ = decimal.TryParse(s, out decimal ret);
        return ret;
    }


    /// <summary>
    /// 字符串转字节数组
    /// </summary>
    public static byte[] Bytes(this string s)
    {
        return Encoding.UTF8.GetBytes(s);
    }

    /// <summary>
    /// 字节数组转字符串
    /// </summary>
    public static string String(this byte[] bs)
    {
        return Encoding.ASCII.GetString(bs);
    }
    
}